<template>
  <div class="app-container">

    <u-page title="导购申请">

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="申请类型" prop="type">
            <el-select v-model="listQuery.type" placeholder="请选择" clearable>
              <el-option value="" label="全部" />
              <el-option :value="0" label="入职申请单" />
              <el-option :value="1" label="离职申请单" />
              <el-option :value="2" label="转正申请单" />
            </el-select>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="listQuery.name" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="身份证" prop="idCard">
            <el-input v-model="listQuery.idCard" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="商户" prop="merchantName">
            <el-input v-model="listQuery.merchantName" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="审批状态" prop="status">
            <el-select v-model="listQuery.status" placeholder="请选择状态" class="status" clearable>
              <el-option value="" label="全部" />
              <el-option :value="0" label="初审中" />
              <el-option :value="2" label="初审失败" />
              <el-option :value="1" label="复审中" />
              <el-option :value="4" label="复审失败" />
              <el-option :value="3" label="已结束" />
            </el-select>
          </el-form-item>
          <el-form-item label="申请时间">
            <u-date-time-picker v-model="searchDate" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button v-if="row.status === 1 && row.type === 0" type="text" @click="hdlCreate(row)">工牌</el-button>
              <el-button type="text" @click="hdlEdit(row)">查看详情</el-button>
              <el-button v-if="row.status === 1" type="text" @click="hdlApproval(row)">审批</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <modal v-model="dialogFormVisible" :data="dataForm" />
    <approval v-model="dialogApprovalVisible" :data="approvalForm" @change="hdlList" />

    <!-- 工牌生成 -->
    <u-dialog title="工牌生成" :show.sync="dialogBadgeVisible" width="410px" :footer="false">

      <div v-if="data.badgeVisible" ref="capture" class="badge">
        <span class="company">{{ data.company }}</span>
        <div class="avatar-placeholder">
          <img class="avatar" :src="data.avatarUrl" :fit="data.avatarfit" alt="头像">
        </div>
        <span class="name">{{ data.name }}</span>
        <img class="corner-image" :src="data.cornerImage" :fit="data.fit" alt="角落图片">
      </div>
      <el-button type="primary" style="margin-top: 20px;" @click="saveAsImage()">保存为图片</el-button>
    </u-dialog>
  </div>
</template>
<script>

import modal from './components/modal'
import approval from './components/approval'
import html2canvas from 'html2canvas'
export default {
  components: {
    modal,
    approval
  },
  data() {
    return {
      api: this.$u.api.PluginsGuideApply,
      // 表格
      columns: [
        {
          prop: 'type',
          label: '申请类型',
          render: (h, params) => {
            const { type } = params.row
            return h('div', type === 0 ? '入职申请单' : type === 1 ? '离职申请单' : '转正申请单')
          }
        },
        {
          prop: 'guideType',
          label: '员工类型',
          render: (h, params) => {
            const { guideType } = params.row
            return h('div', guideType === 0 ? '正式员工' : '试用员工')
          }
        },
        {
          prop: 'name',
          label: '姓名'
        },
        {
          prop: 'mobile',
          label: '联系电话'
        },
        {
          prop: 'idCard',
          label: '身份证号'
        },
        {
          prop: 'merchantName',
          label: '商户'
        },
        {
          prop: 'floor',
          label: '楼层'
        },
        {
          prop: 'createdAt',
          label: '申请时间',
          timestamp: true
        },
        {
          prop: 'status',
          label: '审批状态',
          render: (h, params) => {
            const { status } = params.row
            if (status === 0) {
              return h('el-tag', { props: { type: 'info' }}, '初审中')
            } else if (status === 1) {
              return h('el-tag', { props: { type: '' }}, '复审中')
            } else if (status === 2) {
              return h('el-tag', { props: { type: 'danger' }}, '初审失败')
            } else if (status === 3) {
              return h('el-tag', { props: { type: 'success' }}, '已结束')
            } else if (status === 4) {
              return h('el-tag', { props: { type: 'danger' }}, '复审失败')
            }
          }
        },
        {
          prop: 'note',
          label: '审批备注'
        }
      ],

      list: [],
      listLoading: false,
      listOptions: {
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        type: '',
        endTime: '',
        name: '',
        mobile: '',
        status: ''
      },
      dialogApprovalVisible: false,
      approvalForm: {},

      dialogStatus: '',
      textMap: {
        update: '编辑导购',
        create: '新增导购'
      },
      dialogFormVisible: false,
      dataForm: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: ['blur'] }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: ['blur'] }
        ]
      },
      newForm: {},

      // 删除选中数据
      selectData: [],
      active: 'first',
      dialogBadgeVisible: false,
      // 表格
      data: {
        avatarfit: '',
        cornerImagefit: '',
        name: '',
        company: '',
        cornerImage: 'https://csms.chiconysquare.net/assets/logo/brandlogo.png',
        badgeVisible: true,
        avatarUrl: ''
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    saveAsImage() {
      const element = this.$refs.capture
      html2canvas(element).then((canvas) => {
        const link = document.createElement('a')
        link.href = canvas.toDataURL('image/png')
        link.download = 'badge-image.png'
        link.click()
      })
    },
    // 工牌创建
    hdlCreate(row) {
      console.log(row)
      this.dialogBadgeVisible = true
      // this.data = row
      this.data.avatarfit = 'cover'
      this.data.cornerImagefit = ''
      this.data.name = row.name
      this.data.company = row.merchantName
      this.data.cornerImage = 'https://csms.chiconysquare.net/assets/logo/brandlogo.png'
      this.data.avatarUrl = 'https://csms.chiconysquare.net/' + row.avatar

      // this.data.avatarUrl = ' http://j.dsik.cn/' + row.avatar
      this.data.badgeVisible = true
    },
    // 编辑
    hdlEdit(row) {
      this.dataForm = row
      this.dialogFormVisible = true
    },
    hdlApproval(row) {
      this.approvalForm = row
      this.dialogApprovalVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{

  & >>> .el-drawer__body {
    overflow: auto;
  }

  .uploader{

    .thumb{
      display: inline-block;
      text-align: center;
      outline: none;
      background-color: #fbfdff;
      border: 1px dashed #c0ccda;
      border-radius: 6px;
      box-sizing: border-box;
      width: 120px;
      height: 120px;
      cursor: pointer;
      line-height: 120px;
      vertical-align: top;

      ::v-deep {
        .el-image {
          width: 100%;
          height: 100%;
          display: block;
          border-radius: 6px;
        }

        img{
          width: 100%;
          height: 100%;
          display: block;
          border-radius: 6px;
        }
      }
    }
  }

}

.badge {
  border: 1px solid #000;
  border-radius: 5px;
  width: 371px;
  height: 602px;
  //padding: 20px;
  text-align: center;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}
.company {
  font-weight: bold;
  margin-top: 50px; /* 下移50px */
  display: -webkit-box; /* 使其成为弹性盒子 */
  -webkit-box-orient: vertical; /* 设置主轴方向为纵向 */
  -webkit-line-clamp: 2; /* 限制显示的行数为 2 */
  overflow: hidden; /* 隐藏多余内容 */
  transition: font-size 0.3s ease; /* 字体大小变化的过渡效果 */
  font-size: 40px; /* 默认字体大小 */
  color: black;
}

.name {
  font-size: 55px; /* 放大一倍 */
  font-weight: bold;
  color: black;
}
.avatar-placeholder {
  width: 245px; /* 椭圆形宽度 */
  height: 280px; /* 椭圆形高度 */
  border: 2px solid #000;
  border-radius: 50%; /* 椭圆形 */
  margin: 20px auto;
  position: relative;
  overflow: hidden;
}
.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
}
.corner-image {
  position: absolute;
  width: 147px;
  height: 49px;
  top: 20px;
  right: 20px;
  object-fit: cover; /* 确保图像适应框 */
}
</style>
